<template>
  <div class="person">
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="姓名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="性别">
        <el-radio-group v-model="form.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="年龄">
        <el-input v-model="form.age"></el-input>
      </el-form-item>

      <el-form-item label="联系方式">
        <el-input v-model="form.tel"></el-input>
      </el-form-item>

      <div class="salesmansPosition">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:3000/images/uploadImages"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="comImg" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>

      <el-form-item class="c">
        <el-button type="primary" @click="onSubmit">确认修改</el-button>
         <el-button type="primary" @click="modifyChange">修改密码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("salesmans");
export default {
  data() {
    return {
      form: {
        name: "",
        sex: "",
        age: "",
        tel: "",
      },
      imageUrl: "",
    };
  },
  computed: {
    comImg() {
      return `http://localhost:3000/temp/${this.imageUrl}`;
    },
  },
  async created() {
    //用JSON将本地存储输出的字符串转换为对象
    const re=JSON.parse(localStorage.loginUser);
    const data = await this.getSalesmansDataById({
      _id: re._id,
    });
  

    this.form.name = data.name;
    this.form.sex = data.sex;
    this.form.age = data.age;
    this.form.tel = data.tel;
    this.imageUrl = data.images;
  },
  methods: {
    ...mapActions(["getSalesmansDataById",'modifySalesmansById']),
    onSubmit() {
      //用JSON将本地存储输出的字符串转换为对象
      const re=JSON.parse(localStorage.loginUser);
    
      this.modifySalesmansById({
        _id:re._id,
        Data: {
          username:re.username,
         
          name: this.form.name,
          sex: this.form.sex,
          age: this.form.age,
          tel: this.form.tel,
          images: this.imageUrl,
        },
      });
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      // this.imageUrl = res.data[0];

      if (res.status) {
        this.imageUrl = res.data[0];
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    modifyChange()
    {
      this.$router.push('/System/SalesmansPersonallyPassword');
    }
  },
};
</script>

<style lang="scss" scoped>
.person {
  position: relative;
}

.person .el-input {
  width: 40% !important;
}

.person .el-form-item {
  margin-top: 40px;
}

.person .salesmansPosition {
  position: absolute;
  top: 0px;
  left: 780px;
}
.person .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  margin-left: 195px;
}
.person .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.person .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.person .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.person .el-form-item {
  margin-left: 140px !important;
}
</style>